from django.views import View
from django.shortcuts import render
from django.shortcuts import HttpResponse
from django.http.response import JsonResponse
from api import models
from web.forms.auction import AuctionForm
from web.forms.auction import AuctionItemModelForm
from datetime import datetime
from mycelery.sms.tasks import send_email
import uuid
class AuctionListView(View):

    def get(self,request):
        queryset=models.Auction.objects.all()
        return render(request,'auction/auction_list.html',{'queryset':queryset})



class AuctionAddView(View):
    '''
    后台管理 添加 拍卖专场
    '''
    def get(self,request):
        form=AuctionForm()


        return render(request,'auction/auction_change.html', {'form': form})
    def post(self,request):

        form=AuctionForm(data=request.POST,files=request.FILES)
        if form.is_valid():
            instance=form.save()
            preview_datetime = datetime.utcfromtimestamp(instance.preview_start_time.timestamp())
            send_email.apply_async(args=['牛有道'],eta=preview_datetime)
            return HttpResponse('添加成功')
        else:
            return JsonResponse({'error':form.errors})

class AuctionEditView(View):
    '''
    编辑项目专场视图
    '''
    def get(self,request,pk):
        try:
            auction_obj=models.Auction.objects.filter(pk=pk).first()
            form=AuctionForm(instance=auction_obj)
            return render(request,'auction/auction_change.html',{'form':form})
        except Exception:
            return HttpResponse('获取拍卖专场失败！')
    def post(self,request,pk):
        try:
            auction_obj = models.Auction.objects.filter(pk=pk).first()
            form = AuctionForm(instance=auction_obj,data=request.POST,files=request.FILES)
            if form.is_valid():
                form.save()
                return HttpResponse('修改成功')
        except Exception:
            return HttpResponse('获取拍卖专场失败！')

class AuctionDeleteView(View):
    '''
    删除拍卖专场
    '''
    def get(self,request,pk):
        try:
            auction_obj = models.Auction.objects.filter(pk=pk).delete()
            return HttpResponse('删除成功！')
        except Exception:
            return HttpResponse('删除失败')

class AuactionDetailView(View):
    '''
    拍卖专场详情
    '''
    def get(self,request,pk):
        auction_obj = models.Auction.objects.filter(pk=pk).first()
        auction_items=models.AuctionItem.objects.filter(auction=auction_obj)
        return render(request,'auction/auction_detail.html',{'auction':auction_obj,'auction_items':auction_items})


class AuctionItemAddView(View):
    def get(self,request,pk):
        auction_obj = models.Auction.objects.filter(pk=pk).first()
        form=AuctionItemModelForm()
        return render(request,'auction/auction_item_add.html',{'auction':auction_obj,'form':form})
    def post(self,request,pk):
        auction_obj = models.Auction.objects.filter(pk=pk).first()
        form = AuctionItemModelForm(data=request.POST,files=request.FILES)
        if form.is_valid():
            form.instance.auction=auction_obj
            form.instance.uid=str(uuid.uuid4())
            instance=form.save()
            return HttpResponse('添加完成')
        else:
            return render(request, 'auction/auction_item_add.html', {'auction': auction_obj, 'form': form})